/**
 * 品牌
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @date 2018/11/10
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','jquery','zmnSelect','zmnApp', 'zmnBind'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let table = layui.table;
    let laypage = layui.laypage;
    let zmnBind = layui.zmnBind;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;
    form.render(null, "zmn-form");
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-265',
        done:function () {

        }
    });

    laypage.render(zmnPage.laypageOptions(json));

    // 部门下拉组件
    let deptTree = function (orgId) {
        if (orgId == null) {
            orgId = 0; // 后端Integer类型接收
        }
        zmnSelect.deleteXmSelect("deptId");
        let objMap = zmnSelect.select({
            'deptId': {
                isRadio: true, //单选标识：true-单选；false-多选
                selectedValues: [json.deptId == null ? '' : json.deptId], //已选中值（用于回显)
                tree: {
                    enable: true,
                    //是否展示三角图标
                    showFolderIcon: true,
                    //非严格模式
                    strict: false,
                },
                propMap: {  //选项参数映射配置
                    name: "deptName",   //显示值
                    value: "deptId"  //选中值
                },
                remote: { //远程获取下拉数据
                    enable: true, //开关：true-开启；false-关闭
                    url: json.ZMN_URL_ENV + 'mcc.xiujiadian.com/common/dept/listDeptTree.action?orgId=' + orgId, //服务url
                },
            }
        });
        objMap['deptId'].update({
            filterable: false,
            autoRow: false,
            model: {label: {type: 'text'}},
            on: function (data) {
                if (data.isAdd) {
                    return data.change.slice(0, 1)
                }
            }
        })
    }

    function plat(){
        // 组织架构
        zmnBind.selectl(form, json.ZMN_URL_ENV + 'mcc.xiujiadian.com/common/organize/listChilds', 'plat', 'orgTwoId', 'orgId', [json.plat, json.orgTwoId, json.orgId], [function () {
            return {parentId: 1}
        }, function () {
            return {parentId: zmnUtil.emptyToNull($("#plat").val())}
        }, function () {
            return {parentId: zmnUtil.emptyToNull($("#orgTwoId").val())}
        }], [
            function (data) {
                if (appUtil.notNull(data)) {
                    deptTree(zmnUtil.emptyToNull($("#plat").val()));
                }
            },
            function (data) {
                if (appUtil.notNull(data)) {
                    deptTree($("#orgTwoId").val());
                }
            },
            function (data) {
                if (appUtil.notNull(data)) {
                    deptTree($("#orgId").val());
                }
            }
        ], null, false);
    }
    plat();
    // 加载部门组件 回显部门数据
    if (appUtil.notNull(json.orgId)) {
        deptTree(json.orgId);
    } else if (appUtil.notNull(json.orgTwoId)) {
        deptTree(json.orgTwoId);
    } else {
        deptTree(json.plat);
    }

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        let ids = table.checkStatus('zmn-table').data.map((item) => item.ruleId);
        modifyStatus(obj,ids);
    });

    $('body').on('click','.modifyDeptStatus',function () {
        let threshold = $(this).attr("thresholdid");
        let event = $(this).attr("event");
        let ids = new Array();
        ids.push(threshold);
        let obj = new Object();
        obj.event = event;
        modifyStatus(obj,ids);
    });

    function modifyStatus(obj,ids){
        if (ids.length === 0) {
            return layer.alert("请选择至少一条数据！");
        }
        switch (obj.event) {
            case 'stop':
                layer.confirm('确定停用吗？', function () {
                    let url = "/brs/rule/threshold/disable.action";
                    ajaxSimple(url,ids);
                });
                break;
            case 'start':
                layer.confirm('确定启用吗？', function () {
                    let url = "/brs/rule/threshold/enable.action";
                    ajaxSimple(url,ids);
                });
                break;
        }
    }

    function ajaxSimple(url, ids){
        $.ajax({
            type: "POST",
            url: url,
            dataType: "json",
            data: {
                ids: ids.join(",")
            },
            success: function (data) {
                if (data.status == 200) {
                    layui.layer.msg(data.message,{"icon": 1,"time":1000},function (){
                        location.reload();
                    });
                } else {
                    layui.layer.msg(data.message, {"icon": 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layui.layer.msg("状态修改异常！", {"icon": 2});
            }
        });
    }

    $(".zmn-form-cancel").click(function(){
        let obj = {
            "plat": ""
            ,"orgTwoId": ""
            ,"orgId": ""
            ,"searchName": ""
            ,"deptId": ""
        };
        json.deptId = null;
        json.plat = null;
        deptTree(json.plat);
        plat();
        form.val('zmn-form',JSON.parse(JSON.stringify(obj)));
    });

    //对外暴露的接口
    exports('brs_order_rule_calls_list', {});
});

function ruleInfo(ruleId){
    layui.layer.open({
        type: 2,
        title: '配置对象规则',
        area: ['550px', '402px'],
        content: '/brs/objective/rule/ruleInfo.action?ruleId=' + ruleId
    });
}